﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Lib.Common;
using Microsoft.ApplicationBlocks.Data;
using Module.EntitiesInfo;

namespace Module.DataAccess
{
    public class PartnerDB
    {
        #region ConnectionString
        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        public PartnerDB()
        {
            ConnectionString = ConfigurationManager.ConnectionStrings["localsql"].ToString();
        }       

        #endregion

        public virtual DataTable GetAll()
        {
            DataTable dt = SqlHelper.ExecuteDataset(ConnectionString, CommandType.StoredProcedure, "Partners_GetAll").Tables[0];
            return dt;

        }
        public virtual void Delete(int id)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Partners_Delete", parameters);
        }

        public virtual int Insert(PartnerInfo info)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{				
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Description", info.Description),
                new SqlParameter("@Status", info.Status)
			};
            return (int)SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Partners_Insert", parameters);                      
        }

        public virtual void Update(PartnerInfo info)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", info.Id),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Description", info.Description),
                new SqlParameter("@Status", info.Status)
			};
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Partners_Update", parameters);            
        }

        public virtual IDataReader GetInfo(int id)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Partners_GetInfo", parameters);           
        }

        /// <summary>
        /// Tìm kiếm
        /// </summary>
        /// <param name="keysearch">Tên đối tác</param>
        /// <param name="starDatetime">Ngày bắt đầu</param>
        /// <param name="endDatetime">Ngày kết thúc</param>
        /// <returns></returns>
        public virtual DataTable Search(string keysearch, DateTime starDatetime, DateTime endDatetime)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{                
                new SqlParameter("@Keysearch", keysearch),                  
                new SqlParameter("@StarDatetime", starDatetime),
                new SqlParameter("@EndDatetime", endDatetime)                
			};

            return SqlHelper.ExecuteDataset(ConnectionString, CommandType.StoredProcedure, "Partners_Search", parameters).Tables[0];
        }
    }    
}

